/**
 * Copyright (C), 2018 store Encoding: UTF-8 Date: 19-9-16 下午1:27 History:
 */
package com.swak.frame.ratelimit.adapter;

import com.swak.frame.ratelimit.SwakRateLimiter;
import com.swak.frame.ratelimit.config.LimitOperation;

/**
 * 限流适配器
 * 
 * @ClassName: LimitRaterFactoryAdapter.java
 * @author: colley.ma
 * @date: 2022/01/24
 */
public class LimitRaterFactoryAdapter implements LimitRaterAdapter {

	private SwakRateLimiter swakRateLimiter;

	public LimitRaterFactoryAdapter() {
	}

	public LimitRaterFactoryAdapter(SwakRateLimiter swakRateLimiter) {
		this.swakRateLimiter = swakRateLimiter;
	}

	public void setSwakRateLimiter(SwakRateLimiter swakRateLimiter) {
		this.swakRateLimiter = swakRateLimiter;
	}

	@Override
	public boolean tryAcquire(LimitOperation config) {
		if (swakRateLimiter == null) {
			return true;
		}
		return swakRateLimiter.tryAcquire(config.getResource());
	}

	@Override
	public boolean tryAcquire(String resource) {
		if (swakRateLimiter == null) {
			return true;
		}
		return swakRateLimiter.tryAcquire(resource);
	}
}
